const { executeQuery, baseQuery } = require('./mongo_util')
const collectionName = "goods";


//获取单条数据
function getgoods(goods, onRetGot, onErrGot) {
    console.log(goods);
    let id = goods.id * 1;

    executeQuery(collectionName, (collection, callback) => {
        collection.findOne({"id":id}).then((res) => {
            console.log(res);
            onRetGot(res);
            callback()
        })
            .catch((err) => {
                onRetGot(err);
                callback();
        })
    })
}
//根据分类获取所有分类数据
function category(goods, onRetGot, onErrGot) {
    console.log(goods);
    let limit = goods.size*1;
    let skip = (goods.page - 1) * limit;
    let category = goods.category;
    console.log(limit,skip);
    executeQuery(collectionName, (collection, callback) => {
        collection.find({ "category": category }).count().then(async(res) => {
            let obj = {};
            obj.total = res;

            await collection.find({ "category": category }).skip(skip).limit(limit).toArray().then((bbq => {
                obj.data = bbq;
            }))
            onRetGot(obj);
            callback()
        })
            .catch((err) => {
                onRetGot(err);
                callback();
        })
    })
}


//根据字段排序
function sort(goods, onRetGot, onErrGot) {
    console.log(goods);
    let limit = goods.size*1;
    let skip = (goods.page - 1) * limit;
    let ss = goods.sort;
    let sort = {};
    sort[ss]=-1
    console.log(sort);
    executeQuery(collectionName, (collection, callback) => {
        collection.find({}).count().then(async(res) => {
            let obj = {};
            obj.total = res;

            await collection.find({}).sort(sort).skip(skip).limit(limit).toArray().then((bbq => {
                obj.data = bbq;
            }))
            onRetGot(obj);
            callback()
        })
            .catch((err) => {
                onRetGot(err);
                callback();
        })
    })
}





//返回所有数据
function goodsall(goods, onRetGot, onErrGot) {
    console.log(goods);
    let limit = goods.size*1;
    let skip = (goods.page - 1) * limit;
    console.log(limit,skip);
    executeQuery(collectionName, (collection, callback) => {
        collection.find({}).count().then(async(res) => {
            let obj = {};
            obj.total = res;
            await collection.find({}).skip(skip).limit(limit).toArray().then((bbq => {
                obj.data = bbq;
            }))
            onRetGot(obj);
            callback()
        })
            .catch((err) => {
                onRetGot(err);
                callback();
        })
    })
}




//获取模糊查询数据
function select(goods, onRetGot, onErrGot) {
    console.log(goods);
    let name = goods.name;
    let limit = goods.size*1;
    let skip = (goods.page - 1) * limit;
    const x= new RegExp(name);
    console.log(x);
    console.log("data=",name,limit,skip);
    executeQuery(collectionName, (collection, callback) => {
        collection.find({"name":x}).count().then(async(res) => {
            let obj = {};
            obj.total = res;
            await collection.find({"name":x}).skip(skip).limit(limit).toArray().then((bbq => {
                obj.data = bbq;
            }))
            onRetGot(obj);
            callback()
        })
            .catch((err) => {
                onRetGot(err);
                callback();
        })
    })
}




//增加商品
function addgoods(goods, onRetGot, onErrGot) {
    console.log(goods);
    executeQuery(collectionName, (collection, callback) => {
        collection.insertOne(goods).then((res) => {
            console.log("res=",res);
            onRetGot(res);
            callback()
        })
            .catch((err) => {
                onRetGot(err);
                callback();
        })
    })
}


//根据id删除商品数据
function deletegoods(goods, onRetGot, onErrGot) {
    let ids = goods.id;

    console.log("ids",ids);
    executeQuery(collectionName, (collection, callback) => {
        collection.deleteMany({"id":{$in:ids}}).then((res) => {
            console.log("res=",res);
            onRetGot(res);
            callback()
        })
            .catch((err) => {
                onRetGot(err);
                callback();
        })
    })
}




//修改商品
function updategoods(goods, onRetGot, onErrGot) {
    let id = goods.id;
    let data = goods.data;
    console.log("goods",goods);
    console.log("id",id);
    console.log("data=",data);
    executeQuery(collectionName, (collection, callback) => {
        collection.update({"id":id},{$set:data}).then((res) => {
            console.log("res=",res);
            onRetGot(res);
            callback()
        })
            .catch((err) => {
                onRetGot(err);
                callback();
        })
    })
}

module.exports = {
    getgoods,
    category,
    deletegoods,
    addgoods,
    updategoods,
    select,
    goodsall,
    sort

}